from time import time
import hashlib
import base64


def generate_signature(*args):
    # 获取当前时间戳（秒级）
    timestamp = str(round(time.time()))

    # 组合所有参数并添加时间戳
    args_list = list(args) + [timestamp]

    # 计算 SHA1 哈希值
    sha1_hash = hashlib.sha1(",".join(args_list).encode()).hexdigest()

    # 拼接哈希值和时间戳
    signature_string = f"{sha1_hash},{timestamp}"

    # Base64 编码
    encoded_signature = base64.b64encode(signature_string.encode()).decode()

    return encoded_signature


def generate_signature_v2(*args) -> str:
    # 获取当前时间戳（秒级）
    timestamp = str(int(time()))

    # 将所有参数转换为字符串并添加时间戳
    args_list = [str(arg) for arg in args] + [timestamp]

    # 计算 SHA1 哈希值
    sha1_hash = hashlib.sha1(",".join(args_list).encode()).hexdigest()

    # 拼接 SHA1 哈希值和时间戳
    signature_string = f"{sha1_hash},{timestamp}"

    # Base64 编码
    encoded_signature = base64.b64encode(signature_string.encode()).decode()
    return encoded_signature

# 测试示例
#print(generate_signature("/api/movie/ZWYzNCN0ZXVxMGJ0dWEjKC01N3cxcTVvNS0takA5OHh5Z2ltbHlmeHMqLSFpLTAtbWIx"))
print(generate_signature_v2("/api/movie"))
